layui.use(['layer', 'carousel','form'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var carousel = layui.carousel;
    var device = layui.device;
    var form = layui.form;
    var buttomId = "全局变量";

    /**
     * 系统管理--用户管理
     */
    var MgrUser = {
        tableId: "userTable",    //表格id
        condition: {
            name: "",
            deptId: "",
            timeLimit: ""
        }
    };

    // 渲染轮播
    carousel.render({
        elem: '.layui-carousel',
        width: '100%',
        height: '60px',
        arrow: 'none',
        autoplay: true,
        trigger: device.ios || device.android ? 'click' : 'hover',
        anim: 'fade'
    });

    $(function () {

        /*点击草稿日程框架栏的更多*/
        $("#draftSframe").click(function () {
            Feng.success("正在跳转到草稿日程框架页面");
            window.location.href="/myScheduleDraft/loadSframeDraft";
        });

        /*点击草稿日程表栏的更多*/
        $("#draftSform").click(function () {
            Feng.success("正在跳转到草稿日程表页面");
            window.location.href="/myScheduleDraft/loadSformDraft";
        });

        //加载页面信息
        $.post({
            url:"/myScheduleDraft/showMyScheduleDraft",
            success:function (json) {

                var draftScheduleForms =json.draftScheduleForms;
                var draftScheduleFrames=json.draftScheduleFrames;
                //用于sform拼接
                var form="";
                //用于sframe拼接
                var frame="";
                var sformStatus="checked";
                var sframeStatus="checked";
                var publish='off';

                for (var i=0;i<draftScheduleForms.length;i++) {
                    var creatTime= createTime(draftScheduleForms[i].creatTime);
                    if (i>=3) {
                        break;
                    }



                    if (draftScheduleForms[i].isShare!="on"){
                        sformStatus="";
                    }
                   form+="<div class=\"layui-col-xs12 layui-col-sm6 layui-col-md3 \" id=\"show-btn\">\n" +
                       "    <div class=\"layui-card layui-height-lwh\" id=\"show1\" style='background-color: #00B0E8'>\n" +
                       "        <div class=\"box layui-card-background-lwho\">\n" +
                       "            <div class=\"box-content\">\n" +
                       "                <div class=\"layui-card-header layui-height-lwh-header\">\n" +"" +
                       "                <input type=\"text\" id='selectId' value='"+draftScheduleForms[i].sformId+"' hidden />"+
                       "                    <label>创建时间："+creatTime+"</label>\n" +
                       "                </div>\n" +
                       "                <div class=\"layui-card-body layui-height-lwh-body\">\n" +
                       "                    <p class=\"lay-big-font\">"+draftScheduleForms[i].sformName+"</p>\n" +
                       "                    <p><span class=\"pull-right\">\n" +
                       "                                                  <input type=\"checkbox\" name=\"my_schedule_open_1\" lay-filter=\"myfirst_share\"\n" +
                       "                                                         id='"+draftScheduleForms[i].sformId+" ' lay-skin='switch' lay-text=\"公开|私有\" "+sformStatus+" ></span></p>\n" +
                       "                </div>\n" +
                       "                <ul   class=\"icon\">\n" +
                       "                    <li >\n" +
                       "                        <a  lay-filter='updateBtn' id='updateBtn"+draftScheduleForms[i].sformId+"'href='javaScript:openRevise("+draftScheduleForms[i].sformId+")' > <i class=\"fa fa-magic\"></i></a>\n" +
                       "                    </li>\n" +
                       "                    <li>\n" +
                       "                        <a href='/myScheduleForm/checkScheduleForm?sformId="+draftScheduleForms[i].sformId+"'  id='check'><i class=\"fa fa-check\"></i></a>\n" +
                       "                    </li>\n" +
                       "                    <li>\n" +
                       "                        <a  id='combine' href='javaScript:combineSform("+draftScheduleForms[i].sformId+")'  ><i class=\"fa fa-legal\"></i></a>\n" +
                       "                    </li>\n" +
                       "                    <li>\n" +
                       "                        <a  id='remove'href='javaScript:isDelete("+draftScheduleForms[i].sformId+")' ><i class=\"fa fa-remove\"></i></a>\n" +
                       "                </ul>\n" +
                       "            </div>\n" +
                       "        </div>\n" +
                       "    </div>\n" +
                       "</div>"

                }
                sformStatus="checked";


                for (var i=0;i<draftScheduleFrames.length;i++) {

                    if (i>=3) {
                        break;
                    }

                    var creatTime= createTime(draftScheduleFrames[i].creatTime);

                    if (draftScheduleFrames[i].publish!="on"){
                        sframeStatus="";
                        publish='true';
                    }

                    frame+="<div class=\"layui-col-xs12 layui-col-sm6 layui-col-md3 \" id=\"show-btn\">\n" +
                        "    <div class=\"layui-card layui-height-lwh\" id=\"show1\">\n" +
                        "        <div class=\"box layui-card-background-lwho\">\n" +
                        "            <div class=\"box-content\">\n" +
                        "                <div class=\"layui-card-header layui-height-lwh-header\">\n" +
                        "                    <label>创建时间："+creatTime+"</label>\n" +
                        "                </div>\n" +
                        "                <div class=\"layui-card-body layui-height-lwh-body\">\n" +
                        "                    <p class=\"lay-big-font\">"+draftScheduleFrames[i].sframeName+"</p>\n" +
                        "                    <p><span class=\"pull-right\">\n" +
                        "                                                  <input type=\"checkbox\" name=\"my_schedule_open_1\" lay-filter=\"myfirst_share\"\n" +
                        "                                                         id='"+draftScheduleFrames[i].sframeId+" ' lay-skin='switch' lay-text=\"公开|私有\" "+sframeStatus+"  ></span></p>\n" +
                        "                </div>\n" +
                        "                <ul class=\"icon\">\n" +
                        "                    <li>\n" +
                        "                        <a href='javaScript:copySframe("+draftScheduleFrames[i].sframeId+")' ><i class=\"fa fa-copy\"></i></a>\n" +
                        "                    </li>\n" +
                        "                    <li>\n" +
                        "                        <a href='javaScript:reviseSframe("+draftScheduleFrames[i].sframeId+","+publish+")' ><i class=\"fa fa-magic\"></i></a>\n" +
                        "                    </li>\n" +
                        "                    <li>\n" +
                        "                        <a href='javaScript:deleteSframe("+draftScheduleFrames[i].sframeId+")' ><i class=\"fa fa-remove\"></i></a>\n" +
                        "                    </li>\n" +
                        "                </ul>\n" +
                        "            </div>\n" +
                        "        </div>\n" +
                        "    </div>\n" +
                        "</div>"


                }
                sframeStatus="checked";
                publish='off';

                $("#showsfrom").html(form);
                $("#showsframe").html(frame);

                layui.use('form', function() {
                    var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
                    form.render();
                });
            }
        });




/*        //修改日程表
        $('#updateBtn').click(function () {
            alert("dfhafd");
            layer.open({
                type:2,
                area: ['500px', '300px'],
                title: '修改日程',
                content: Feng.ctxPath + '/myScheduleForm/reviseSform',
                tableId: MgrUser.tableId
            });
        });*/





/*
        //删除
        $('#remove').on('click', function(){
            var othis = $(this);
            alert(othis);
            alert("graeh")
            layer.confirm('您是确定要删除该日程表吗？', {
                    btn: ['确定','取消'] //按钮
                },
                function(){
                    layer.msg('确定', {icon: 1});
                    $.post({
                        url:"/myScheduleForm/deleteSformBySformId?sformId="+$("#selectId").valueOf(),
                        success:function (data) {
                            console.log(data);
                            if (data.valueOf()=='删除日程表成功！') {
                                                           Feng.success(data.valueOf()+"正跳转到日程表页面");
                                                         /!*   /!*!//关闭当前窗口*!/
                                                            admin.closeThisDialog();
                                parent.location.href="/myScheduleForm/showScheduleForm";

                            }else {
                                Feng.error(data.valueOf());
                            }
                        }
                    });

                });
        });

*/




    });


    $('#cancel,#cancel2,#cancel3,#cancel4').on('click', function(){
        var othis = $(this);
        layer.open({
            content: '您确认取消认购？',
            btn: ['确认', '取消'],
            shadeClose: false,
            yes: function(){
                layer.open({content: '确认取消认购', time: 1});
                othis.parents('.atxt').remove();
            }, no: function(){
                layer.open({content: '您选择了取消', time: 1});
            }
        });
    });


    form.on('#remove',function () {
        var othis = $(this);
        alert(othis);
        alert("graeh")
        layer.confirm('您是确定要删除该日程表吗？', {
                btn: ['确定','取消'] //按钮
            },
            function(){
                layer.msg('确定', {icon: 1});
                $.post({
                    url:"/myScheduleForm/deleteSformBySformId?sformId="+$("#selectId").valueOf(),
                    success:function (data) {
                        console.log(data);
                        if (data.valueOf()=='删除日程表成功！') {
                            Feng.success(data.valueOf()+"正跳转到日程表页面");
                            /*   /!*!//关闭当前窗口*/
                            admin.closeThisDialog();
                            parent.location.href="/myScheduleForm/showScheduleForm";

                        }else {
                            Feng.error(data.valueOf());
                        }
                    }
                });

            });

    });

    form.on('switch(myfirst_share)', function(data){
        alert("ASgG")
        if (data.elem.checked){
            layer.msg(data.elem.id,{time:5000});
            alert(data.value)
        };
        var sformId=data.elem.id;
        var status=data.elem.checked;
        $.ajax({
            url:"/myScheduleForm/setShare?sformId="+sformId+"&status="+status,
            type:"get",
            dataType:"json",
            success:function(data){
                console.log(data);
                layer.msg(data.msg);
            },
            error:function(data){
                $.messager.alert('错误',data.msg);
            }
        });
        // layer.close(index);
    });




    });


function createTime(v){
    var date = new Date(v);
    var y = date.getFullYear();
    var m = date.getMonth()+1;
    m = m<10?'0'+m:m;
    var d = date.getDate();
    d = d<10?("0"+d):d;
    var h = date.getHours();
    h = h<10?("0"+h):h;
    var M = date.getMinutes();
    M = M<10?("0"+M):M;
    var str = y+"-"+m+"-"+d+" "+h+":"+M;
    return str;
}




